<template>
  <!-- 位置使用方 -->
  <div class="recommendConfig">
    <div class="update">
      <StaryeaLoading v-if="tableData.loading"></StaryeaLoading>
      <div class="nameList">
        <div class="name">已配置厂商</div>
        <div class="list" v-if="!disabledType">
          <el-button type="text" @click="updatePosition('top')" :disabled="disabled">置顶</el-button>
          <el-button type="text" @click="updatePosition('moveUp')" :disabled="disabled">上移</el-button>
          <el-button type="text" @click="updatePosition('moveDown')" :disabled="disabled">下移</el-button>
          <el-button type="text" @click="updatePosition('bottom')" :disabled="disabled">置底</el-button>
          <el-button type="text" style="color:red;" :disabled="JSON.stringify(this.selectRecom)=='{}'?true:false" @click="deleteData">删除</el-button>
        </div>
      </div>
      <div class="radio">
        <el-checkbox v-model="checked" @change='onChange' :disabled="disabledType">独占模式</el-checkbox>
      </div>
      <div class="table" v-if="tableData.data.length>0">
        <!-- header -->
        <div class="colums">
          <div class="colItem" v-for="(item,idx) in tableData.columns" :key="'h'+idx">
            {{item.title}}
          </div>
        </div>
        <!-- bady -->
        <div class="dataBox" :class="item.isChecked?'color':''"
          v-for="(item,index) in tableData.data" :key="'b'+index" @click="selectData(item)">
          <!-- 单选 -->
          <div class="selcet" :class="item.isChecked?'isChecked':''"></div>
          <div class="item">{{item.sequence}}</div>
          <div class="item">{{item.providerId}}</div>
          <div class="item">{{item.providerName}}</div>
        </div>
      </div>
      <div v-if="tableData.data.length<=0"
        style="height:60px;display:flex;color:rgb(205, 205, 205);justify-content:center;align-items:center;">
        暂未配置厂商
      </div>
      <!-- 按钮 -->
      <div class="btn" v-if="!disabledType">
        <el-button round type="primary" plain @click="addFirm" :disabled="isDisabled">新增厂商</el-button>
      </div>
    </div>

    <!-- 新增厂商弹窗 -->
    <Modal v-model="editModal.show" :title="editModal.title" :width="900">
      <StaryeaLoading v-if="modalTable.loading"></StaryeaLoading>
      <div class="seachBox">
        <div class="boxFlex">
          <div class="items">
            <div class="title">厂商ID</div>
            <div class="cont">
              <el-input v-model="seachData.providerId" placeholder="请输入厂商ID" clearable style="width:200px;">
              </el-input>
            </div>
          </div>
          <div class="items">
            <div class="title">厂商名称</div>
            <div class="cont">
              <el-input v-model="seachData.providerName" placeholder="请输入厂商名称" clearable style="width:200px;">
              </el-input>
            </div>
          </div>
        </div>
        <div class="btn"><Button type="primary" @click="queryFirmData(seachData)">查询</Button></div>
      </div>
      <div class="table">
        <!-- header -->
        <div class="colums">
          <div class="colItem" v-for="(item,idx) in modalTable.columns" :key="'h'+idx">
            {{item.title}}
          </div>
        </div>
        <!-- bady -->
        <div class="dataBox" :class="item.isChecked?'color':''"
          v-for="(item,index) in modalTable.data" :key="'b'+index" @click="selectFirm(item)">
          <!-- 单选 -->
          <div class="selcet" :class="item.isChecked?'isChecked':''"></div>
          <div class="item">{{item.providerId}}</div>
          <div class="item">{{item.providerName}}</div>
          <div class="item">{{item.createTime}}</div>
        </div>
      </div>
      <Page :total="modalTable.total" :current="modalTable.current" :page-size="modalTable.pageSize"
        @on-page-size-change="changePageSize" @on-change="changePage"
        show-elevator show-sizer show-total transfer
        style="text-align:right;margin-top: 20px;"/>
      <div slot="footer">
        <Button @click="handleCancle">取消</Button>
        <Button type="primary" @click="handleOk">确定</Button>
      </div>
    </Modal>
  </div>
</template>

<script>
export default {
  name: "recommendConfig",
  props: {
    disabledType:{
      type: Boolean,
      required: true,
    },
    positionInfo:{//海报位
      type: Object,
      required: true,
      default:() =>{}
    },
    floorComList:{//楼层组件列表
      type: Object,
      required: true,
      default:() =>{}
    }
  },
  data () {
    return {
      checked: true,// 0：共享  1：独占
      disabled: false,
      isDisabled: false,
      // table
      tableData: {
        columns: [
          { title:'',type:'index' },
          { title:'排序',key:'index' },
          { title:'厂商ID',key:'id' },
          { title:'厂商名称',key:'name' },
        ],
        data: [],
        loading: false,
      },
      selectRecom:{},
      // 新增弹窗
      editModal:{
        show: false,
        title: '',
      },
      // 弹窗table
      modalTable: {
        columns:[
          { type:'',type:'index' },
          { title:'厂商ID',key:'providerId' },
          { title:'厂商名称',key:'providerName' },
          { title:'创建时间',key:'createTime' },
        ],
        data:[],
        loading: false,
        total: 0,
        current: 1,
        pageSize: 10,
      },
      // 查询
      seachData:{},
      rexommendObj: {}
    }
  },
  methods: {
    // 初始化查询配置项
    initQueryRecomm(){
      let _this = this;
      let params= {floorPageId: this.positionInfo.floorId, sitePageId: this.positionInfo.siteId};
      this.tableData.loading = true;
      requestInfoConfig(params,'selectPosition').then(res =>{
        let {result_code, result_msg,data} = res;
        if(result_code == 0){
          this.tableData.loading = false;
          if(data.providerArr.length>0){
            data.providerArr.forEach(item =>{
              item.isChecked = false;
              this.selectRecom = {};
            });
            // 独占模式的控制
            if(data.monopolyFlag==1 && data.providerArr.length>=1){
              _this.isDisabled = true;
            } else {
              _this.isDisabled = false;//新增可用
            }
            // 移动按钮控制
            if(data.providerArr.length == 1){
              _this.disabled = true;
            } else {
              _this.disabled = false;
            }
            _this.checked = data.monopolyFlag==1?true:false;
            this.tableData.data = data.providerArr;
          } else {
            _this.tableData.data = [];
            // 移动按钮控制
            _this.disabled = true;
            _this.checked = data.monopolyFlag==1?true:false;
            _this.isDisabled = false;//新增可用
          }
        } else {
          this.$Message.error(result_msg || this.$cfg.COMMONERRORMSG);
        }
      })
    },
    // 是否独占
    onChange(data){
      if(this.hasBtn('add_site_provide')==false){
        this.$message({message:'您没有设置厂商独占的权限，若要设置请联系管理员！',type:'error'});
      } else {
        if(this.tableData.data.length > 1){
          this.checked = false;
          this.$message({message:'独占设置失败，请删除多余配置',type:'warning'});
        } else {
          data = data==false?0:1;
          this.isDisabled = false;
          if(this.tableData.data.length<=0){
            this.isDisabled = false;
          } else if(data == 1 && this.tableData.data.length>=1){
            this.isDisabled = true;
          }
          let params ={ floorPageId: this.positionInfo.floorId, sitePageId:this.positionInfo.siteId, monopolyFlag:data };
          requestInfoConfig(params,'onlyType').then(res =>{
            let {result_code, result_msg} = res;
            if(result_code == 0){
              this.$message({message:'保设置成功',type:'success'});
            } else {
              this.$Message.error(result_msg || this.$cfg.COMMONERRORMSG);
            }
          })
        }
      }
    },
    // 置顶:top,置底:bottom,上移:moveUp,下移:moveDown
    updatePosition(type){
      if(this.hasBtn('add_site_provide')==false){
        this.$message({message:'您没有移动厂商的权限，若要移动请联系管理员！',type:'error'});
      } else {
        if(JSON.stringify(this.selectRecom) == '{}'){
          this.$message({message:'请选择要移动的厂商',type:'warning'});
        } else {
          let params = {
            providerId: this.selectRecom.providerId,
            sitePageId: this.positionInfo.siteId,
            floorPageId: this.positionInfo.floorId,
            seqType: type,
          }
          this.loading = true;
          requestInfoConfig(params,'seqPosition').then(res =>{
            let {result_code, result_msg} = res;
            if(result_code == 0){
              this.loading = false;
              this.initQueryRecomm(this.positionInfo.siteId);
              this.$message({message: '移动成功',type:'success'});
            } else {
              this.$Message.error(result_msg || this.$cfg.COMMONERRORMSG);
            }
          })
        }
      }
    },
    // 选中已配置数据
    selectData(data){
      if(this.disabledType == true) return ;
      this.selectRecom = data;
      this.tableData.data.forEach(item =>{
        if(data.providerId == item.providerId){
          item.isChecked = true;
        } else {
          item.isChecked = false;
        }
      });
    },
    // 删除
    deleteData(){
      if(this.hasBtn('add_site_provide')==false){
        this.$message({message:'您没有删除厂商的权限，若要删除请联系管理员！',type:'error'});
      } else {
        let _this = this;
        if(JSON.stringify(this.selectRecom) == '{}'){
          this.$message({message:'请选择要删除的厂商',type:'warning'});
        } else {
          this.$Modal.confirm({
            title: '删除提醒',
            content: '确定删除该条内容？',
            loading: true,
            onOk: function () {
              let params = {
                sitePageId: _this.positionInfo.siteId,
                floorPageId: _this.positionInfo.floorId,
                providerId: _this.selectRecom.providerId
              }
              requestInfoConfig(params,'deleteProvider').then(res =>{
                let {result_code, result_msg} = res;
                if(result_code == 0){
                  this.$message({message:'删除成功',type:'success'});
                  this.$Modal.remove();
                  _this.selectRecom = {};
                  _this.initQueryRecomm(_this.positionInfo.siteId);
                } else {
                  this.$Message.error(result_msg || this.$cfg.COMMONERRORMSG);
                }
              })
            }
          })
        }
      }
    },

    /******** 新增 ********/
    // 新增厂商
    addFirm(){
      // if(this.hasBtn('add_site_provide')==false){
      //   this.$message({message:'您没有新增厂商的权限，若要新增请联系管理员！',type:'error'});
      // } else {
        this.editModal.show = true;
        this.editModal.title = '选择厂商';
        this.seachData = {
          floorPageId: this.positionInfo.floorId,
          sitePageId: this.positionInfo.siteId,
          providerName:'',
          providerId:'',
          start: 0,
          length:10,
        }
        this.queryFirmData();
      // }
    },
    // 查询厂商库
    queryFirmData(param){
      let _this = this;
      if (typeof param === 'object') { Object.assign(_this.modalTable, param) }
      _this.seachData.start = (_this.modalTable.current - 1) * _this.modalTable.pageSize;
      _this.seachData.length = _this.modalTable.pageSize;
      _this.modalTable.loading = true;//加载状态
      requestInfoConfig(_this.seachData,'selectPro').then(res =>{
        let {result_code, result_msg,data,recordsTotal} = res;
        if(result_code == 0){
          _this.modalTable.loading = false;
          data.forEach(item =>{ item.isChecked = false; })
          _this.modalTable.data = data;
          _this.modalTable.total = recordsTotal;
        } else {
          this.$Message.error(result_msg || this.$cfg.COMMONERRORMSG);
        }
      })
    },
    // 选择厂商
    selectFirm(data){
      this.rexommendObj = data;
      this.modalTable.data.forEach(item =>{
        if(data.providerId == item.providerId){
          item.isChecked = true;
        } else {
          item.isChecked = false;
        }
      })
    },
    // 保存新增
    handleOk(){
      if(JSON.stringify(this.rexommendObj) == '{}'){
        this.$message({message:'请选择要添加的厂商',type:'warning'});
      } else {
        let params ={
          sitePageId: this.positionInfo.siteId,
          floorPageId: this.positionInfo.floorId,
          providerId: this.rexommendObj.providerId
        }
        requestInfoConfig(params,'addPosition').then(res =>{
          let {result_code, result_msg} = res;
          if(result_code == 0){
            this.$message({message:'保存成功',type:'success'});
            this.editModal.show = false;
            this.initQueryRecomm(this.positionInfo.siteId);
          } else {
            this.$Message.error(result_msg || this.$cfg.COMMONERRORMSG);
          }
        })
      }
    },
    // 关闭弹窗
    handleCancle(){
      this.editModal.show = false;
    },
    // 分页
    changePageSize(value) {
      // console.log("changePageSize",value);
      this.queryFirmData({
        pageSize: value,
        current: 1
      })
    },
    changePage(value) {
      // console.log("changePage",value);
      this.queryFirmData( {
        current: value
      })
    },
  },
}
</script>

<style lang="less" scoped>
.recommendConfig {
  .update{
    .nameList{
      display: flex;
      align-items: center;
      justify-content: space-between;
      margin: 15px 20px;
      .name{
        font-weight: bold;
      }
    }
    .radio{
      margin-left: 20px;
      margin-bottom: 10px;
    }
    .table{
      .colums{
        background-color: #F0F2F5;
        padding: 0px !important;
        .colItem{
          margin: 8px 0;
          width: 105px;
          text-align: center;
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;
          &:nth-child(1){
            margin: 8px 10px;
            width: 15px;
            height: 15px;
          }
          &:nth-child(2){
            width: 30px;
          }
        }
      }
      .colums, .dataBox{
        display: flex;
        align-items: center;
        justify-content: space-around;
        border: 1px solid #e8eaec;
        border-bottom: none;
      }
      .dataBox{
        &:nth-last-child(1){
          border: 1px solid #e8eaec;
        }
        .item{
          margin: 13px 0;
          width: 105px;
          text-align: center;
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;
          &:nth-child(2){
            width: 30px;
          }
        }
        .selcet{
          display: inline-block;
          position: relative;
          border: 1px solid #dcdfe6;
          border-radius: 2px;
          box-sizing: border-box;
          margin: 10px;
          width: 15px;
          height: 15px;
          background-color: #fff;
          z-index: 1;
          transition: border-color .25s cubic-bezier(.71,-.46,.29,1.46),background-color .25s cubic-bezier(.71,-.46,.29,1.46);
          &::after{
            box-sizing: content-box;
            content: "";
            border: 1px solid #fff;
            border-left: 0;
            border-top: 0;
            height: 7px;
            left: 4px;
            position: absolute;
            top: 1px;
            transform: rotate(45deg) scaleY(0);
            width: 3px;
            transition: transform .15s ease-in .05s;
            transform-origin: center;
          }

        }
        .isChecked {
          background-color: #409eff;
          border-color: #409eff;
          &::after{
            transform: rotate(45deg) scaleY(1);
          }
        }
      }
      .color{
        color: #409eff;
      }
    }
    .btn{
      margin-top: 20px;
      display: flex;
      justify-content: space-evenly;
      align-items: center;
    }
  }
}
</style>

<style lang="less" scoped>
// input
/deep/ .el-input__inner{
  padding: 0px 10px;
  height: 35px;
  line-height: 35px;
}
// textarea
/deep/ .el-textarea__inner{
  resize:none;
  padding: 5px 10px;
}
// 按钮
/deep/ .el-button.is-round{
  padding: 8px 40px;
}
/deep/ .el-button.is-disabled, .el-button.is-disabled:focus, .el-button.is-disabled:hover{
  color: #C0C4CC !important;
  cursor: not-allowed;
}
/deep/ .el-button--primary{
  background-color: #3089fd;
  border-color: #3089fd;
}
/deep/ .el-button--primary:focus, .el-button--primary:hover{
  background: #66b1ff;
  border-color: #66b1ff;
  color: #FFF;
}
/deep/ .el-button--primary.is-plain{
  color: #409EFF;
  background: #ecf5ff;
  border-color: #b3d8ff;
}
/deep/ .el-button--primary.is-plain:focus, .el-button--primary.is-plain:hover{
  background: #409EFF;
  border-color: #409EFF;
  color: #FFF;
}
// 搜索
/deep/ .ivu-modal-body .seachBox{
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-top: 10px;
  margin-bottom: 40px;
  .boxFlex{
    display: flex;
    align-items: center;
    .items{
      display: flex;
      align-items: center;
      &:nth-child(2){
        margin: 0 15px;
      }
      .title{
        width: 70px;
        text-align: right;
        margin-right: 10px;
      }
    }
  }
  .btn{
    margin-right: 30px;  
  }
}
// 表格
/deep/ .ivu-modal-body .table{
  .colums{
    background-color: #F0F2F5;
    padding: 0px !important;
    .colItem{
      padding: 8px 0;
      width: 400px;
      text-align: center;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
      border-right: 1px dashed #cdcdcd;
      &:nth-child(1){
        border-right: none;
        margin-right: 0;
        margin: 8px 10px;
        width: 15px;
        height: 15px;
      }
      &:nth-child(2){
        width: 70px;
        padding-right: 20px;
      }
      &:nth-child(4){
        width: 300px;
      }
      &:nth-last-child(1){
        border-right: none;
        margin-right: 0;
      }
    }
  }
  .colums, .dataBox{
    display: flex;
    align-items: center;
    justify-content: space-around;
    border: 1px solid #e8eaec;
    border-bottom: none;
  }
  .dataBox{
    &:nth-last-child(1){
      border: 1px solid #e8eaec;
    }
    .item{
      margin: 13px 0;
      width: 400px;
      text-align: center;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
      &:nth-child(2){
        width: 70px;
        padding-right: 20px;
      }
      &:nth-child(4){
        width: 300px;
      }
    }
    .selcet{
      display: inline-block;
      position: relative;
      border: 1px solid #dcdfe6;
      border-radius: 2px;
      box-sizing: border-box;
      margin: 10px;
      width: 15px;
      height: 15px;
      background-color: #fff;
      z-index: 1;
      transition: border-color .25s cubic-bezier(.71,-.46,.29,1.46),background-color .25s cubic-bezier(.71,-.46,.29,1.46);
      &::after{
        box-sizing: content-box;
        content: "";
        border: 1px solid #fff;
        border-left: 0;
        border-top: 0;
        height: 7px;
        left: 4px;
        position: absolute;
        top: 1px;
        transform: rotate(45deg) scaleY(0);
        width: 3px;
        transition: transform .15s ease-in .05s;
        transform-origin: center;
      }

    }
    .isChecked {
      background-color: #409eff;
      border-color: #409eff;
      &::after{
        transform: rotate(45deg) scaleY(1);
      }
    }
  }
  .color{
    color: #409eff;
  }
}
</style>
